package com.neusoft.weixin.web.account;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.neusoft.common.util.Page;
import com.neusoft.weixin.WebController;
import com.neusoft.weixin.service.account.ContentService;

@Controller
@RequestMapping(value = "/content")
public class ContentController {
	
	@Autowired
	protected ContentService contentService;
	
	private Logger log = LoggerFactory.getLogger(ContentController.class);
	
	@RequestMapping(value = "listContent")
	@ResponseBody
	public Object listContent(ServletRequest request,ServletResponse response) throws IOException {
		try
		{
			//前台grid返回的参数（参数名称不用改）
			String pageNo = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			
			String messageId = request.getParameter("messageId");
			//查询条件集合
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("MESSAGE_ID", messageId);
			//total 前台grid总数的name，rows 前台grid数据集的name
			Page page = new Page("total","rows",pageNo,pageSize);

			return contentService.getGridData(page, condition);
		} catch (Exception e)
		{
			log.error(e.getMessage());
			return null;
		}
		
	}
	
	@RequestMapping(value = "upload",method = RequestMethod.POST)
	@ResponseBody
	public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.info("保存上传的图片");
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
		MultipartFile image = multiRequest.getFile("file");
		
		String path = request.getSession().getServletContext().getRealPath("/web/imgs");
		String sourceName = image.getOriginalFilename();
		String postfix = sourceName.substring(sourceName.lastIndexOf("."));
		String fileName = new Date().getTime()+postfix;
		File destFile = new File(path, fileName);
		image.transferTo(destFile);
		
		return "/imgs/"+fileName;
	}
	
	@RequestMapping(value = "remove",method = RequestMethod.POST)
	@ResponseBody
	public String remove(HttpServletRequest request, HttpServletResponse response) {
		
		log.info("删除上传的图片");
		String filePath = request.getParameter("imgUrl");
		String path = request.getSession().getServletContext().getRealPath("/web");
		File image = new File(path, filePath);
		image.delete();
		
		return "success";
	}
	
	@RequestMapping(value = "delContent",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject delContent(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject data = JSONObject.fromObject(request.getParameter("data"));
		log.info("删除内容信息");
		String CONTENT_ID = data.getString("CONTENT_ID");
		contentService.deleteContent(CONTENT_ID);
		log.info("删除图片");
		String path = request.getSession().getServletContext().getRealPath("/web");
		if ( data.has("CONTENT_IMG") && !"".equals(data.getString("CONTENT_IMG")) ){
			String CONTENT_IMG = data.getString("CONTENT_IMG");
			File image = new File(path, CONTENT_IMG);
			if( image.delete() ){
				log.info("删除图片成功");
			}
		}
		
		log.info("删除静态页面");
		if ( data.has("CONTENT_URL") && !"".equals(data.getString("CONTENT_URL")) ){
			String CONTENT_URL = data.getString("CONTENT_URL");
			File html = new File(path, CONTENT_URL);
			if( html.delete() ){
				log.info("删除静态页面成功");
			}
		}
		
		JSONObject result = new JSONObject();
		result.put("status", "1");
		result.put("detail", "操作成功!");
		
		
		return result;
	}
	
	@RequestMapping(value = "save",method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		log.info("保存内容信息");
		String CONTENT_ID = request.getParameter("CONTENT_ID");
		String CONTENT_TITLE = request.getParameter("CONTENT_TITLE");
		String CONTENT_IMG = request.getParameter("CONTENT_IMG");
		String CONTENT_DESC = request.getParameter("CONTENT_DESC");
		String CONTENT_URL = request.getParameter("CONTENT_URL");
		String CONTENT = request.getParameter("CONTENT");
		
		JSONObject json = new JSONObject();
		json.put("CONTENT_ID", CONTENT_ID == null ? "" : CONTENT_ID);
		json.put("CONTENT_TITLE", CONTENT_TITLE == null ? "" : CONTENT_TITLE);
		json.put("CONTENT_IMG", CONTENT_IMG == null ? "" : CONTENT_IMG);
		json.put("CONTENT_DESC", CONTENT_DESC == null ? "" : CONTENT_DESC);
		json.put("CONTENT_URL", CONTENT_URL == null ? "" : CONTENT_URL);
		json.put("CONTENT", CONTENT == null ? "" : CONTENT);
		
		
		
		JSONObject result = contentService.addOrEditContent(json);
		return result.get("status").toString();
	}
	
	
	@RequestMapping(value = "createHtml",method = RequestMethod.POST)
	@ResponseBody
	public String createHtml(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String content = request.getParameter("CONTENT");
		String title = request.getParameter("CONTENT_TITLE");
		Map<String,String> map = new HashMap<String, String>();
		map.put("title", title);
		map.put("content", content);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		map.put("date", sdf.format(new Date()));
		
		String fileName = contentService.createHtml(map, request);
		
		return fileName;
	}
	
	
	
}
